package com.druid.request;

import cn.hutool.core.util.StrUtil;
import com.druid.util.ManagementException;
import com.druid.util.UtilForData;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@SuppressWarnings("unused")
@Data
@ApiModel(description = "" +
        "图片AI请求对象。根据传入的参数分别调用三种不同的openai的api接口。对应关系如下：" +
        "1.image、rectangle、prompt不为空，请求CRATE_IMAGE_EDIT（根据文字提示生成图片）" +
        "2.image不为空，请求CREATE_IMAGE_VARIATION（根据上传图片和文字提示生成编辑图片）" +
        "3.prompt不为空，请求CRATE_IMAGE（根据图片变化出多种类型的图片）")
public class PicAiRequest {

    @ApiModelProperty(value = "图片文件", required = true)
    private MultipartFile image;

    @ApiModelProperty(
            value = "矩形数据(传入json字符串)startX表示截图开始的图像坐标X，startY表示图像坐标Y，width表示矩形的宽度，height表示矩形的高度。"
            ,example = "{\n" +
            "          startX: X,\n" +
            "          startY: Y,\n" +
            "          width: W,\n" +
            "          height: H\n" +
            "        }")
    private String rectangle;

    @JsonIgnore
    private Rectangle rectangleObject;

    @ApiModelProperty(value = "图片模式（0-图片动态联想；1-有截取原图相关性；2-无截取原图相关性）", example = "mode1", allowableValues = "'0', '1', '2'")
    private String picModel;

    @ApiModelProperty(value = "提示信息")
    private String prompt;

    @ApiModelProperty(value = "图片数量(最大4张)", example = "1")
    private int picCount;

    @ApiModelProperty(value = "生成图片尺寸", example = "1024x1024")
    private String size;

    /**
     * 根据所传参数判断图片操作类型
     */
    public picApiType getPicApiType(){

        if ( rectangleObject==null && StrUtil.isNotBlank(rectangle) ) {
            rectangleObject = UtilForData.transJsonStringToT(rectangle,Rectangle.class);
        }

        if ( image != null && rectangleObject != null && StrUtil.isNotBlank(prompt) ) {
            return picApiType.CRATE_IMAGE_EDIT;
        }

        if ( image != null ) {
            return picApiType.CREATE_IMAGE_VARIATION;
        }

        if ( StrUtil.isNotBlank(prompt) ) {
            return picApiType.CRATE_IMAGE;
        }

        throw new ManagementException("参数不全");
    }

    public Rectangle getRectangleObject() {
        if (rectangleObject==null) {
            return UtilForData.transJsonStringToT(rectangle,Rectangle.class);
        }
        return rectangleObject;
    }

    @Data
    public static class Rectangle {

        private double startX;
        private double startY;
        private double width;
        private double height;
    }
    /**
     * 图片操作类型
     */
    public enum picApiType{

        CRATE_IMAGE,            //  根据文字提示生成图片
        CRATE_IMAGE_EDIT,       //  根据上传图片和文字提示生成编辑图片
        CREATE_IMAGE_VARIATION  //  根据图片变化出多种类型的图片
    }

}



